package edu.shzu.manage.controller;

import edu.shzu.manage.entity.Role;
import edu.shzu.manage.service.RoleService;
import edu.shzu.manage.util.Result;
import edu.shzu.manage.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class RoleController {
    @Autowired
    RoleService roleService;

    @GetMapping("/roles")
    public Result getRole() {
        List<Role> roleList = roleService.getRole();
        if (roleList.size() > 0) {
            return Result.success(roleList);
        } else {
            return Result.fail();
        }
    }

    /**
     * 根据ID 查询角色
     * @param id
     * @return
     */
    @GetMapping("/role")
    public Result getRoleById(Integer id) {
        Role resultRole = roleService.getRoleById(id);
        if (resultRole != null) {
            System.out.println(resultRole);
            return Result.success(resultRole);
        } else {
            return Result.fail();
        }
    }

    /**
     * 查询所有角色
     * @return
     */
    @GetMapping("/role/list")
    public Result getAllRoles(){
        List<Role> roleList = roleService.getRole();
        List<RoleVo> roleVoList = new ArrayList<>();
        for (Role role:roleList) {
            RoleVo roleVo = new RoleVo();
            Integer[] menuIds = roleService.getMenuIds(role.getRoleId());
            roleVo.setRoleId(role.getRoleId());
            roleVo.setRoleName(role.getRoleName());
            roleVo.setRemark(role.getRemark());
            roleVo.setMenuIds(menuIds);
            //加入roleVo集合
            roleVoList.add(roleVo);
        }
        if (roleVoList.size() > 0) {
            return Result.success(roleVoList);
        } else {
            return Result.fail();
        }
    }

    /**
     * 向角色表及其关联中间表role_menu中添加数据
     * @param objectMap
     * @return
     */
    @PostMapping("/role/insert")
    @Transactional
    public Result insertRole(@RequestBody Map<String,Object> objectMap) {
        Role newRole = new Role();
        newRole.setRoleName((String) objectMap.get("roleName"));
        newRole.setRemark((String) objectMap.get("remark"));
        int i = roleService.insertRole(newRole);    //先向role表插入新增角色
        if (i > 0) {
            Integer roleId = newRole.getRoleId();  //获取新增的  角色Id
            List<Integer> menuIds = (List<Integer>) objectMap.get("menuIds"); //再向role_menu表中插入角色对应的多个菜单权限
            for(Integer menuId:menuIds) {
                roleService.insertRoleMenu(roleId,menuId);
            }
            return Result.success();
        } else {
            return Result.error(3007,"添加角色失败");
        }
    }

    /**
     * 更新角色
     * @param objectMap
     * @return
     */
    @PostMapping("/role/update")
    public Result updateRole(@RequestBody Map<String,Object> objectMap) {
        Integer roleId = (Integer) objectMap.get("roleId");
        Role newRole = new Role();
        newRole.setRoleId(roleId);
        newRole.setRoleName((String) objectMap.get("roleName"));
        newRole.setRemark((String) objectMap.get("remark"));
        int i = roleService.updateRole(newRole);  //先更新角色表中的信息
        if (i > 0) {
            //2.更新role_menu表中的角色权限，先删再加
            roleService.deleteRoleMenu(roleId);
            System.out.println("roleId" + roleId);
            List<Integer> menuIds = (List<Integer>) objectMap.get("menuIds");
            System.out.println("menuIds[]:" + menuIds);
            for (Integer menuId:menuIds) {
                roleService.insertRoleMenu(roleId,menuId);
            }
            return Result.success();
        } else {
            return Result.error(3008,"更新角色失败");
        }
    }

    /**
     * 根据角色Id 删除角色表及其 关联中间表role_menu中的数据
     * @param id
     * @return
     */
    @DeleteMapping("/role/{id}")
    public Result deleteRole(@PathVariable("id") Integer id) {
        int i = roleService.deleteRole(id);
        if (i > 0) {
            roleService.deleteRoleMenu(id);
            return Result.success();
        } else {
            return Result.error(3009,"删除角色失败");
        }
    }

    /**
     * 模糊查询
     * @param str
     * @return
     */
    @GetMapping("/role/query")
    public Result queryRoles(String str) {
        List<RoleVo> roleVoList = roleService.queryRoles(str);
        if (roleVoList.size() > 0) {
            return Result.success(roleVoList);
        } else {
            return Result.fail();
        }
    }
}
